package it.azienda.managerworkstandard.view;

import it.azienda.framework.persistenza.DAOConnectionSingleton;
import it.azienda.framework.utilita.Utilita;
import it.azienda.framework.view.Dettaglio;
import it.azienda.managerworkstandard.modello.BeanComune;
import it.azienda.managerworkstandard.modello.BeanRubrica;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlForm;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class DettaglioRubrica extends Dettaglio{
    
    private HtmlForm form = new HtmlForm();
    private BeanRubrica beanRubrica;
    
    public void setBeanRubrica(BeanRubrica beanRubrica) {
        setObj(beanRubrica);
        this.beanRubrica = beanRubrica;
    }
    
    public BeanRubrica getBeanRubrica(){
        return (BeanRubrica)getObj();
    }
    
    public HtmlForm getForm() {
        form = (HtmlForm) Utilita.getSession().getAttribute("form");
        if (form == null) {
            form = new HtmlForm();
        }
        FacesContext ctx = FacesContext.getCurrentInstance();
        beanRubrica =(BeanRubrica) ctx.getExternalContext().getRequestMap().get("beanRubrica");
        controlloDopoSalvataggio(beanRubrica);
        Utilita.getSession().removeAttribute("form");
        return form;
    }
    
    public void lookUp(ValueChangeEvent event) {
        if(event.getOldValue() != null && event.getNewValue()!=null ){
            if(!event.getOldValue().toString().toUpperCase().equals(event.getNewValue().toString().toUpperCase())){
                if(event.getNewValue().toString().length() >= 1 ){
                    
                    FacesContext ctx = FacesContext.getCurrentInstance();
                    beanRubrica =(BeanRubrica) ctx.getExternalContext().getRequestMap().get("beanRubrica");
                    setObj(beanRubrica);
                    String descrizione=new String(""+event.getNewValue());
                    
                    String nomeBean=(String)event.getComponent().getAttributes().get("nomeBean");
                    String nomeBeanLookUp =(String)event.getComponent().getAttributes().get("nomeBeanLookUp");
                    
                    if(nomeBean.equals("beanComune")){
                        BeanComune comune=(BeanComune)lookUpRapido(descrizione.toUpperCase(),nomeBeanLookUp);
                        if (comune == null) {
                            ((HtmlInputText) form.findComponent("descrizioneComune")).setSubmittedValue(null);
                            ((HtmlInputText) form.findComponent("cap")).setSubmittedValue(null);
                            beanRubrica.setBeanComune(new BeanComune());
                            setObj(beanRubrica);
                            HtmlForm form = (HtmlForm)ctx.getViewRoot().findComponent("erroriDataBase");
                            form.setRendered(true);
                            ctx.addMessage("erroriDataBase", new FacesMessage("erroriDataBase","Comune inesistente"));
                        } else {
                            beanRubrica.setBeanComune(comune);
                            setObj(beanRubrica);
                            ((HtmlInputText) form.findComponent("descrizioneComune")).setSubmittedValue(comune.getDescrizione());
                        }
                    }
                    ctx.renderResponse();
                }
            }
        }
    }
    
    public void svuotaLookup() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        beanRubrica =(BeanRubrica) ctx.getExternalContext().getRequestMap().get("beanRubrica");
        setObj(beanRubrica);
        
        String nomeBean=Utilita.getRequest().getParameter("bean");
        if(nomeBean.equals("beanComune")){
            ((HtmlInputText) form.findComponent("descrizioneComune")).setSubmittedValue(null);
            ((HtmlInputText) form.findComponent("cap")).setSubmittedValue(null);
            beanRubrica.setBeanComune(new BeanComune());
            setObj(beanRubrica);
        }
        ctx.renderResponse();
    }
    
    public boolean controlliPrimaSalvataggio() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        beanRubrica =(BeanRubrica) ctx.getExternalContext().getRequestMap().get("beanRubrica");
        boolean errori = false;
        
        if(beanRubrica.getBeanComune()!= null) {
            if(beanRubrica.getBeanComune().getId() == null) {
                beanRubrica.setBeanComune(null);
            }
        }
        
        if(errori) {
            controlloDopoSalvataggio(beanRubrica);
            return true;
        }
        return false;
    }
    
    public void controlloDopoSalvataggio(Object obj) {
        BeanRubrica rubrica=(BeanRubrica)obj;
        if(rubrica.getBeanComune()==null){
            rubrica.setBeanComune(new BeanComune());
        }
    }
    
    public void setForm(HtmlForm form) {
        setFormObject(form);
        this.form = form;
        Utilita.getSession().setAttribute("form",form);
    }
    
    public void primaInserimento(Object obj) {
        FacesContext ctx = FacesContext.getCurrentInstance();
        beanRubrica =(BeanRubrica) ctx.getExternalContext().getRequestMap().get("beanRubrica");
        String query=" select max(rubrica.codice) from BeanRubrica rubrica";
        Session session = null;
        Transaction tr = null;
        try {
            session = DAOConnectionSingleton.newSession();
            tr = session.beginTransaction();
            Query q = null;
            q = session.createQuery(query);
            ArrayList listaCodice = (ArrayList) q.list();
            Integer cod=(Integer) listaCodice.get(0);
            if(cod!=null){
                this.beanRubrica.setCodice(new Integer(cod.intValue()+1));
            }else{
                this.beanRubrica.setCodice(new Integer(1));
            }
        } catch (Exception e) {
        }
    }
}
